/*
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2022-02-02 20:48:52
 * @LastEditors: GGB
 * @LastEditTime: 2022-02-05 11:41:23
 */
import { defineStore } from 'pinia'

// useStore could be anything like useUser, useCart
// the first argument is a unique id of the store across your application
export const useStore = defineStore('main', {
  state: () => ({
    isFullScreen: false,
    buycarts: []
  }),
  getters: {
    totalPrice: (state) => state.buycarts.reduce((acc, cur) => acc + cur.num * cur.price, 0)
  },
  actions: {
    /**
     * 设置是否全屏
     * @param {*} status 
     */
    setFullScreen(status) {
      this.isFullScreen = status
    },
    /**
     * 加入购物车
     * @param {*} payload 
     */
    addBuycart(payload) {
      let isExist = this.buycarts.findIndex(v => v.id === payload.id)

      if (isExist === -1) {
        this.buycarts.push(payload)
      } else {
        this.buycarts[payload].num++
      }
    },
    /**
     * 新增购物车数量
     * @param {*} payload 
     */
    addBuycartNum(payload) {
      this.buycarts[payload].num++
    },
    /**
     * 减少购物车数量
     * @param {*} payload 
     */
    reduceBuycartNum(payload) {
      this.buycarts[payload].num--
      if (this.buycarts[payload].num === 0) {
        this.buycarts.splice(payload, 1)
      }
    }
  }
})